package com.tanhua.system.log;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.system.pojo.ActiveUser;
import com.tanhua.system.pojo.UserLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import java.util.Date;

@Slf4j
@Component
@RocketMQMessageListener(topic = "user-sso-operate",
        consumerGroup = "tanhua-sso-consumer")
public class LogConsumer implements RocketMQListener<String> {

    private static ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(String msg) {
        try {
            JsonNode jsonNode = objectMapper.readTree(msg);
            //用户id
            Long userId = jsonNode.get("userId").asLong();
            //登陆时间
            Long date = jsonNode.get("date").asLong();
            //操作类型  1:登录  2:其余操作
            Integer type = jsonNode.get("type").asInt();
            String collectionName = "user_log_" + new DateTime().toString("yyyyMMdd");
            UserLog userLog = new UserLog();
            userLog.setId(ObjectId.get());
            userLog.setUserId(userId);
            if (type == 1) {
                //如果是当天第一次登录,记录进活跃用户表,此时操作时长设置为0,并在用户做请求操作时进行查询修改
                Query query = Query.query(Criteria.where("userId").is(userId));
                long count = mongoTemplate.count(query, UserLog.class, collectionName);
                if (count == 0){
                    ActiveUser activeUser = new ActiveUser();
                    activeUser.setId(ObjectId.get());
                    activeUser.setUserId(userId);
                    String loginDate = new DateTime(new Date(date)).toString("yyyyMMdd");
                    activeUser.setLoginDate(Long.valueOf(loginDate));
                    activeUser.setFirstLoginTime(date);
                    activeUser.setOperateTime(0L);
                    mongoTemplate.save(activeUser);
                }
                userLog.setLoginDate(date);//记录登陆时间
                userLog.setOperateDate(0L);//记录请求操作时间
            }else{
                userLog.setOperateDate(date);//记录请求操作时间
                userLog.setLoginDate(0L);//记录登陆时间
                //修改活跃表用户使用时长
                //拿到活跃用户表中该用户信息
                Query query = Query.query(Criteria.where("userId").is(userId));
                ActiveUser activeUser = mongoTemplate.findOne(query, ActiveUser.class);
                Long operateTime = date - activeUser.getFirstLoginTime();
                //修改使用时长
                Update update = Update.update("operateTime",operateTime);
                mongoTemplate.updateFirst(query,update,ActiveUser.class);
            }
            userLog.setType(type);
            mongoTemplate.save(userLog, collectionName);
            mongoTemplate.save(userLog);
        } catch (Exception e) {
            log.error("保存记录出错", e);
        }
    }
}
